package com.yun.many.auth.model.translator;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.many.auth.dao.entity.User;
import com.yun.many.auth.dao.enums.SexEnum;
import com.yun.many.auth.dao.enums.UserStatusEnum;
import com.yun.many.auth.model.dto.LoginUser;
import com.yun.many.auth.model.req.UserEditReq;
import com.yun.many.auth.model.resp.UserVo;

import java.util.Objects;
import java.util.stream.Collectors;

public class UserTranslator {
    public static User toEntity(UserEditReq req) {
        Long id = req.getId();
        UserStatusEnum status = Objects.isNull(req.getStatus()) ? UserStatusEnum.NORMAL : req.getStatus();
        String password = Objects.isNull(id) ? "1" : req.getPassword();

        User user = new User();
        user.setId(req.getId());
        user.setUsername(req.getUsername());
        user.setPassword(req.getPassword());
        user.setNickname(req.getNickname());
        user.setBirthday(req.getBirthday());
        user.setPhone(req.getPhone());
        user.setEmail(req.getEmail());
        user.setAvatar(req.getAvatar());
        user.setStatus(status);
        user.setSex(SexEnum.of(req.getSex()));
        // 默认密码
        user.setPassword(password);
        return user;
    }

    public static LoginUser toLoginUser(User user) {
        LoginUser loginUser = new LoginUser();
        loginUser.setId(user.getId());
        loginUser.setUsername(user.getUsername());
        loginUser.setPassword(user.getPassword());
        loginUser.setNickname(user.getNickname());
        loginUser.setPhone(user.getPhone());
        loginUser.setEmail(user.getEmail());
        loginUser.setAvatar(user.getAvatar());
        loginUser.setStatus(user.getStatus());
        loginUser.setSex(user.getSex());
        loginUser.setTenantId(user.getTenantId());
        return loginUser;
    }

    public static UserVo toVo(User user) {
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setUsername(user.getUsername());
        userVo.setNickname(user.getNickname());
        userVo.setPhone(user.getPhone());
        userVo.setBirthday(user.getBirthday());
        userVo.setAvatar(user.getAvatar());
        userVo.setEmail(user.getEmail());
        userVo.setStatus(user.getStatus().getValue());
        userVo.setStatusDesc(user.getStatus().getDesc());
        userVo.setSex(user.getSex().getValue());
        userVo.setSexDesc(user.getSex().getDesc());
        userVo.setCreated(user.getCreated());
        userVo.setModified(user.getModified());
        userVo.setCreator(user.getCreator());
        userVo.setModifier(user.getModifier());
        return userVo;
    }

    public static Page<UserVo> toVo(Page<User> userPage) {
        Page<UserVo> userVoPage = new Page<>();
        userVoPage.setRecords(userPage.getRecords().stream().map(UserTranslator::toVo).collect(Collectors.toList()));
        userVoPage.setTotal(userPage.getTotal());
        userVoPage.setSize(userPage.getSize());
        userVoPage.setCurrent(userPage.getCurrent());
        return userVoPage;
    }
}
